| Loops | |
|
T o o l s -Assembler --Script examples ---Simple ---Loops ---Mini arithmetic compiler |
|
This
program generates a stand-alone class that prints "hello world" a
few times when invoked. As usual, start with the class hierarchy and
the initialization code.
;; create a container
(define my-class (make-class-env))
;; specify class hierarchy
(jas-class-setaccess my-class acc-public)
(jas-class-setclass my-class (make-class-cpe "out"))
(jas-class-setsuperclass my-class (make-class-cpe "java/lang/Object"))
;; write a convenience function to append insns as a list.
(define append-code
(lambda (code-part insn-list)
(mapcar (lambda (insn) (jas-code-addinsn code-part insn))
insn-list)))
;; Define code for initializer
;;
(define init-it (make-code))
(append-code
init-it
(quote
((aload_0)
(invokenonvirtual (make-method-cpe "java/lang/Object" "<init>" "()V"))
(return))))
You'll notice that mapcar'ing of the jas-code-addinsn onto
a list of insns, so its a little more convenient to write the
actual code.Now we are ready to create the code body to loop over the printing.
;; this is the code that comprises
;; the actual task, ie the code for
;; main(String argv[]).
;; print a string 5 times
(define doit (make-code))
(append-code
doit
(quote
(
; store references in local
; variables to avoid field/cp
; lookup
(getstatic
(make-field-cpe "java/lang/System" "out" "Ljava/io/PrintStream;"))
(astore_1)
(ldc (make-string-cpe "Hello World"))
(astore_2)
(bipush 5)
(istore_3) ; store loop index in var reg 3
; loop 5 times, printing out
; the string.
(make-label "loop")
; push arguments to function
; on stack and call it
(aload_1) (aload_2)
(invokevirtual
(make-method-cpe "java/io/PrintStream" "println" "(Ljava/lang/String;)V"))
; decrement loop index
(iinc 3 -1)
(iload_3)
(ifne (make-label "loop"))
(return))))
;; set stack/var size
(jas-code-stack-size doit 3)
(jas-code-var-size doit 4)
This piece of code shows how labels are defined from the package.
The make-label function
allows you to insert labels into the body of a code. Labels are
uniquely identified by the name they are given, although you can
choose to reuse labels if you like.
The rest of the code is just general instructions to keep track
of the loop index and call System.out.println at the right places.
You will notice that the
;; first the initializer.
(jas-class-addmethod my-class acc-public "<init>" "()V" init-it ())
;; define the printing code as a static main(String argv[]) method
(jas-class-addmethod my-class
(| acc-static acc-public)
"main" "([Ljava/lang/String;)V" doit ())
;; write it out.
(jas-class-write my-class (make-outputstream "out.class"))
Continuing on by assembling, verifying and running the program
% java scm.driver examples/hworld.jas % ls -l out.class -rw-r--r-- 1 kbs 341 Dec 31 12:05 out.class % javap -verify out Class out succeeds % java out Hello World Hello World Hello World Hello World Hello World % | |
|
T o o l s/Assembler/Script examples Simple | Loops | Mini arithmetic compiler
http://www.sbktech.org/jas-scm-loops.html
Revised: Thu Dec 19 08:51:14 1996 |
|